/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Jun 7, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    public static final boolean DEBUG=false;
     public static Date getCurrentTimeDate(){
        Date date=Calendar.getInstance().getTime();
        return date;
    }
    public static long getCurrentTime(){
        long time=Calendar.getInstance().getTime().getTime() ;
        //System.out.println("drawLines()"+numOfRecord+" take "+(stop-start)*0.001f+" to finish" );
        return time;
    }

    public static String diff(long start, long stop){
        return (stop-start)*0.001f+" seconds";
    }
    public static Date getCurrentDate(){
        Date date=Calendar.getInstance().getTime();
        return date;
    }
    public static String getCurrentTimeString(){
        Date date=getCurrentDate();
        return convertDateToString(date);
    }        /**
     *
     * @return format year-month-day:e.g.:2006-1-24
     */
    public static String getCurrentDateString(){
       /* Date date=getCurrentDate();
        date.getYear()*/
        Calendar cld = Calendar.getInstance();
        int year = cld.get(Calendar.YEAR);
        int mm = cld.get(Calendar.MONTH)+1;
        int day = cld.get(Calendar.DAY_OF_MONTH);
        return year+"-"+mm+"-"+day;
    }


    /**
     * The string can be format to date again
     * @return  format: 1/24/06 4:10pm
     */
    public static String convertDateToString(Date date){
        DateFormat fmt=new SimpleDateFormat();
        String dateSt=fmt.format(date);
        /*if(DEBUG)        {
        try {
            Date d2=fmt.parse(dateSt);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
        }*/
        return dateSt;

    }

    /**
     *
     * @param date     
     * @param format   
     * @return
     */
    public static String convertDateToString(Date date,String format){
        DateFormat fmt=new SimpleDateFormat(format);
        String dateSt=fmt.format(date);

        return dateSt;

    }

    /**
     *  
     * @param time    format 2/8/05 9:14 PM
     * @return   A DATE
     */
     public static Date convertStringToDate(String time){
          Date date=null;
          DateFormat fmt=new SimpleDateFormat();
          try {

                date=fmt.parse(time);// 

                } catch (ParseException e) {
                    e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                }
        return date;
    }

    /**
     *
     * @param time
     * @param format
     * @return
     */
     public static Date convertStringToDate(String time,String format){
          Date date=null;
          DateFormat fmt=new SimpleDateFormat(format);
          try {

                date=fmt.parse(time);// 

                } catch (ParseException e) {
                    e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                }
        return date;
    }



    /**
     *  
     * @param c
     * @return
     */
    public static Calendar getSameWeekDate(Calendar c){
       // c.setTimeZone(new SimpleTimeZone());
        //int nextyear = c.get(Calendar.YEAR)+1;
        //Calendar nextyear1st = getCalendar(nextyear,0,1);// 
        //nextyear1st.add(Calendar.WEEK_OF_YEAR,1);
        //boolean crossmoth=false;
        //if(myc.get(Calendar.MONTH)==c.get(Calendar.MONTH) ){ //not work for Dec 27
        if(c.get(Calendar.MONTH)==11&& c.get(Calendar.WEEK_OF_YEAR)==1 ){  //for Dec 12, if week =1, means next year's 1st week
            //System.out.println(TimeUtils.class.getSimpleName() + " replace "+c.getTime() + " with the next calendar year's 1st day "+nextyear1st.getTime() );
            //return nextyear1st;
            Calendar myc=Calendar.getInstance();
            myc.set(Calendar.YEAR, c.get(Calendar.YEAR ) );
            myc.set(Calendar.MONTH,c.get(Calendar.MONTH ) );
            myc.set(Calendar.WEEK_OF_YEAR,53);
            return myc;
        }
        else{

            return c;
        }



    }
     public static Calendar getSameWeekNextYear(Calendar c){
        int nextyear = c.get(Calendar.YEAR)+1;
        Calendar nextyear1st = getCalendar(nextyear,0,1);// 
        //nextyear1st.add(Calendar.WEEK_OF_YEAR,1);
        //boolean crossmoth=false;
        //if(myc.get(Calendar.MONTH)==c.get(Calendar.MONTH) ){ //not work for Dec 27
        if(c.get(Calendar.MONTH)==11&& c.get(Calendar.WEEK_OF_YEAR)==1 ){  //for Dec 12, if week =1, means next year's 1st week
            System.out.println(TimeUtils.class.getSimpleName() + " replace "+c.getTime() + " with the next calendar year's 1st day "+nextyear1st.getTime() );
            return nextyear1st;
        }
        else{
            return c;

        }

    }

    /**
     *
     * @param year
     * @param month         The first month of the year is <code>JANUARY</code> which is 0; the last depends on the number of months in a year (jin: Dec is 11) .
     * @param dayofmonth    The first day of the month has value 1.
     * @return
     */
    public static  Calendar getCalendar(int year,int month,int dayofmonth){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR ,year);
        c.set(Calendar.MONTH,month);//Dec =11
        c.set(Calendar.DAY_OF_MONTH ,dayofmonth);
        return c;
    }

    public static void print(Calendar c){
        /*c.get(Calendar.YEAR);
        c.get(Calendar.MONTH);//Dec =11
        c.get(Calendar.DAY_OF_MONTH);*/
        System.out.println("Date    "+c.get(Calendar.DATE));
        System.out.println("Day of Month    "+c.get(Calendar.DAY_OF_MONTH) );
        System.out.println("DAY_OF_WEEK    "+c.get(Calendar.DAY_OF_WEEK));
        System.out.println("DAY_OF_WEEK_IN_MONTH    "+c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println("DAY_OF_YEAR    "+c.get(Calendar.DAY_OF_YEAR));
        System.out.println("WEEK_OF_MONTH    "+c.get(Calendar.WEEK_OF_MONTH));
        System.out.println("WEEK_OF_YEAR    "+c.get(Calendar.WEEK_OF_YEAR));
        System.out.println("MONTH    "+c.get(Calendar.MONTH));
        System.out.println("YEAR    "+c.get(Calendar.YEAR ) );
    }

    public static void main(String[] args) {




    }
}
